Open In Colab Weave는 Anthropic Python library를 통해 이루어진 LLM 호출을 weave.init()가 호출된 후 자동으로 추적하고 기록합니다.
설정 없이 Weave에서 Anthropic 모델을 실험해 보고 싶으신가요? LLM Playground를 시도해 보세요.

트레이스

개발 중이나 프로덕션 환경에서 LLM 애플리케이션의 트레이스를 중앙 데이터베이스에 저장하는 것이 중요합니다. 이러한 트레이스는 디버깅에 사용되며, 애플리케이션을 개선하는 데 도움이 되는 데이터셋으로 활용됩니다. Weave는 anthropic-sdk-python에 대한 트레이스를 자동으로 캡처합니다. 라이브러리를 평소와 같이 사용할 수 있으며, weave.init()를 호출하여 시작하세요:
import weave    
# use the anthropic library as usual
import os
from anthropic import Anthropic

# highlight-next-line
weave.init("anthropic_project")

client = Anthropic(
    api_key=os.environ.get("ANTHROPIC_API_KEY"),
)

message = client.messages.create(
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Tell me a joke about a dog",
        }
    ],
    model="claude-3-opus-20240229",
)
print(message.content)
이제 Weave는 Anthropic 라이브러리를 통해 이루어진 모든 LLM 호출을 추적하고 기록합니다. Weave 웹 인터페이스에서 트레이스를 볼 수 있습니다. anthropic_trace.png
우리는 LLM 호출을 추적하기 위해 anthropic Messages.create 메서드를 패치합니다.
이제 Weave는 Anthropic을 통해 이루어진 모든 LLM 호출을 추적하고 기록합니다. Weave 웹 인터페이스에서 로그와 인사이트를 볼 수 있습니다.

자신의 ops로 래핑하기

Weave ops는 실험하는 동안 코드를 자동으로 버전 관리하여 결과를 reproducible하게 만들고, 입력과 출력을 캡처합니다. 간단히 @weave.op()로 장식된 함수를 만들어 Anthropic.messages.create를 호출하면 Weave가 입력과 출력을 추적합니다. 중첩된 예제에서 이를 어떻게 할 수 있는지 살펴보겠습니다:
import weave
import os
from anthropic import Anthropic

# highlight-next-line
weave.init("anthropic_project")
client = Anthropic(
    api_key=os.environ.get("ANTHROPIC_API_KEY"),
)

# highlight-next-line
@weave.op()
def call_anthropic(user_input:str, model:str) -> str:
    message = client.messages.create(
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": user_input,
        }
        ],
        model=model,
    )
    return message.content[0].text

# highlight-next-line
@weave.op()
def generate_joke(topic: str) -> str:
    return call_anthropic(f"Tell me a joke about {topic}", model="claude-3-haiku-20240307")

print(generate_joke("chickens"))
print(generate_joke("cars"))
anthropic_ops.png

더 쉬운 실험을 위해 Model를 만드세요

움직이는 부분이 많을 때 실험을 구성하기는 어렵습니다. Model 클래스를 사용하면 시스템 프롬프트나 사용 중인 모델과 같은 앱의 실험적 세부 사항을 캡처하고 구성할 수 있습니다. 이는 앱의 다양한 반복을 구성하고 비교하는 데 도움이 됩니다. 코드 버전 관리 및 입력/출력 캡처 외에도, Model는 애플리케이션의 동작을 제어하는 구조화된 매개변수를 캡처하여 어떤 매개변수가 가장 잘 작동했는지 쉽게 찾을 수 있게 합니다. Weave Models를 serve, 및 Evaluation와 함께 사용할 수도 있습니다. 아래 예제에서는 modeltemperature로 실험할 수 있습니다. 이 중 하나를 변경할 때마다 새로운 versionJokerModel를 얻게 됩니다.
import weave    
# use the anthropic library as usual
import os
from anthropic import Anthropic
weave.init('joker-anthropic')

class JokerModel(weave.Model): # Change to `weave.Model`
  model: str
  temperature: float
  
  @weave.op()
  def predict(self, topic): # Change to `predict`
    client = Anthropic()
    message = client.messages.create(
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": f"Tell me a joke about {topic}",
        }
        ],
        model=self.model,
        temperature=self.temperature
    )
    return message.content[0].text


joker = JokerModel(
    model="claude-3-haiku-20240307",
    temperature = 0.1)
result = joker.predict("Chickens and Robots")
print(result)
anthropic_model.png

도구 (함수 호출)

Anthropic은 tools 인터페이스를 제공하여 함수를 호출합니다. Weave는 이러한 함수 호출을 자동으로 추적합니다.
message = client.messages.create(
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in San Francisco?",
        }
    ],
    tools=[
        {
            "name": "get_weather",
            "description": "Get the current weather in a given location",
            "input_schema": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA",
                    }
                },
                "required": ["location"],
            },
        },
    ],
    model=model,
)

print(message)
우리는 프롬프트에서 사용한 도구를 자동으로 캡처하고 버전 관리합니다. anthropic_tool.png